package com.xiangban.yanglaodajie_admin.utils;

import com.alibaba.fastjson.JSON;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 工具类
 */
@Slf4j
@Component
public class CommonUtils {
    public static final String common_1="1";
    public static final String common_2="2";
    public static final String common_3="3";
    public static final Long DELAY_TIME = 1000*60*60L;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;

    /**
     *     MD5加密工具
     * @param data
     * @return
     */

    public static String MD5(String data){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            for (byte item: array) {
                stringBuilder.append(Integer.toHexString((item & 0xFF)| 0x100).substring(1,3));
            }
            return stringBuilder.toString().toUpperCase();
        }catch (Exception exception){
        }
        return null;
    }

    public static boolean BizEmpty(Object o){

        return o ==null || o.toString().length()<1;

    }
    public static boolean compare(String str1, String str2) {
        return str1.equals(str2);
    }

    /**
     * 校验用户合法性
     */
    public static void checkUserLegal(Object userId, HttpServletRequest request){
        RedisUtil redisUtil = new RedisUtil();
        if (!compare(redisUtil.get(userId.toString()), request.getHeader("token"))){
           throw new BizException(ApiResultCode.NOT_USER_ROLE_LEGAL);
       }
    }

    /**
     * 获取用户id
     */
    public  String getShopId(HttpServletRequest request){
        String token = RedisUtil.fex_shop + request.getHeader("token");
        String shopInfo = redisUtil.get(token);
        log.info("获取的token："+request.getHeader("token"));
        if (shopInfo.length()>0){
            Map<String,Object> parse = (Map<String, Object>) JSON.parse(shopInfo);
            return parse.get("shopId").toString();
        }
        return null;

    }

    public String getToken(HttpServletRequest request){
        return request.getHeader("token");
    }

    public static String getHostIp(){
        String ip = "";
        try{
            ip = InetAddress.getLocalHost().getHostAddress();
        }catch(UnknownHostException e){
            e.printStackTrace();
        }
        return ip;
    }

    public static String getOrderNo(){
        return new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(new Date()).replace("-", "") + RandomStringUtils.randomNumeric(8);
    }
    public static String getSHA256Str(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }




}
